<?php

/**
 * @file
 * Contains the embed display plugin.
 */

/**
 * A plugin to handle defaults on a view.
 *
 * @ingroup views_display_plugins
 */
class embed_views_plugin_display_embed extends views_plugin_display {
  public $embed_exposed_form = FALSE;

  /**
   * Determine if this display is the 'default' display which contains
   * fallback settings
   */
  function is_default_display() { return FALSE; }

  /**
   * Provide the default summary for options in the views UI.
   *
   * This output is returned as an array.
   */
  function options_summary(&$categories, &$options) {
    // Add all the parent options.
    $options['exposed_block'] = array(
      'category' => 'exposed',
      'title' => t('Exposed form in block'),
      'value' => $this->get_option('exposed_block', TRUE) ? t('Yes') : t('No'),
      'desc' => t('Allow the exposed form to appear in a block instead of the view.'),
    );
    $options['exposed_embed'] = array(
      'category' => 'exposed',
      'title' => theme('advanced_help_topic', array('module' => 'embed_views', 'topic' => 'embed-exposed-filter')) . t('Embed exposed form manually'),
      'value' => $this->get_option('exposed_embed') ? t('Yes') : t('No'),
      'desc' => t('Allow the exposed form to appear in a block instead of the view.'),
    );
    parent::options_summary($categories, $options);
  }

  /**
   * Provide the default form for setting options.
   */
  function options_form(&$form, &$form_state) {
    // Add all the parent options.
    parent::options_form($form, $form_state);
    if ($form_state['section'] == 'exposed_block') {
      $form['exposed_block']['#default_value'] = $this->get_option('exposed_block', TRUE) ? 1 : 0;
    }
    if ($form_state['section'] == 'exposed_embed') {
      $form['#title'] .= t('Display exposed filter with embed code');
      $form['description'] = array(
        '#markup' => '<div class="description form-item">' . t('If set, any exposed widgets will not appear with this view. Instead, of displaying any exposed wigets in the render of the view.') . '</div>',
      );
      $form['exposed_embed'] = array(
        '#type' => 'radios',
        '#options' => array(1 => t('Yes'), 0 => t('No')),
        '#default_value' => $this->get_option('exposed_embed') ? 1 : 0,
      );
    }
  }

  /**
   * Perform any necessary changes to the form values prior to storage.
   * There is no need for this function to actually store the data.
   */
  function options_submit(&$form, &$form_state) {
    parent::options_submit($form, $form_state);
    $section = $form_state['section'];
    if ('exposed_embed' == $section) {
      $this->set_option($section, (bool) $form_state['values'][$section]);
    }
  }

  /**
   * Render any special blocks provided for this display.
   */
  function view_special_blocks($type) {
    if ($type == '-exp') {
      // avoid interfering with the admin forms.
      if (arg(0) == 'admin' && arg(1) == 'structure' && arg(2) == 'views') {
        return;
      }
      $this->view->init_handlers();

      if ($this->uses_exposed() && $this->get_option('exposed_block', TRUE)) {
        $exposed_form = $this->get_plugin('exposed_form');
        return array(
          'content' => $exposed_form->render_exposed_form(TRUE),
        );
      }
    }
  }

  /**
   * Intelligently get an option either from this display or from the
   * default display, if directed to do so.
   */
  function get_option($option, $block = FALSE) {
    if ($option == 'exposed_block') {
      if (!$block) {
        return $this->get_option('exposed_embed');
      }
    }
    return parent::get_option($option);
  }
}
